# COMO FSCP Voter By TJOpenLab

# Makefile compilation: `make` in root directory

all: como_fscp_voter voter_client client_sender lib_como_fscp_voter

# Compile parameters CFLAGS
# CFLAGS = -O2 -Wall -W -lpthread
# During debugging, set CFLAGS to -O0 (not optimized) and enable -g
CFLAGS = -O0 -Wall -W -lpthread -g
CC = $(CROSS_COMPILE)gcc
ARCH := $(shell uname -m)

# 表决数据有头部数据，这个头数据不参与表决
CFLAGS += -DBIG_VOTING_DATA_HEAD_SIZE=0

ifeq ($(TjComoFscpVoter),TjComoFscpVoter)
CFLAGS += -DSYNC_WITH_PHXPAXOS=1
endif

# -march flag for x86/ARMv8
ifeq ($(CROSS_COMPILE),aarch64-openeuler-linux-)
    MARCH_FLAG = -march=armv8-a
	CFLAGS += -DRUN_ON_ARMv8
else
	ifeq ($(ARCH),x86_64)
	    MARCH_FLAG = -march=native
	    CFLAGS += -DRUN_ON_X86
	else ifeq ($(ARCH),aarch64)
	    MARCH_FLAG = -march=armv8-a
		CFLAGS += -DRUN_ON_ARMv8
	else
	    $(error Unsupported architecture: $(ARCH))
	endif
endif

# Check whether it is in a WSL environment
IS_WSL := $(shell if [ -n "$(WSL_INTEROP)" ]; then echo "true"; else echo "false"; fi)

ifeq ($(IS_WSL), true)
    CFLAGS2 = -D_IN_WSL_LINUX_
endif

ifeq ($(TjComoFscpVoter),TjComoFscpVoter)
INC = -I./libzmtp/include -I./libcrc -I./inih -I./src -I./rudp -I./tiny-regex-c \
	  -I $(ROOT)/external/phxpaxos/export_como
else
INC = -I./libzmtp/include -I./libcrc -I./inih -I./src -I./rudp -I./tiny-regex-c
endif

SRC = src/config.c                  \
	  src/voter_server.c            \
	  src/voter_comm.c              \
	  src/voter_lib.c               \
	  src/math_utils.c              \
	  src/proc_utils.c              \
	  src/voter_message.c           \
	  libcrc/crc32.c                \
	  inih/ini.c                    \
	  tiny-regex-c/re.c             \
	  src/ZMQ_Switcher.c            \
	  src/voter_logger.c            \
	  src/voter_customized.c        \
	  rudp/rudp.c                   \
	  src/voter_VotingAlgorithms.c  \
	  src/lamport_clock.c           \
	  src/ip_rtnetlink_multi.c      \
	  src/voter_monitor.c

SRC_MAIN = src/como_fscp_voter.c

SRC_LIBZMTP = \
	  ./libzmtp/src/MemPool.c               \
	  ./libzmtp/src/zmtp_channel.c          \
	  ./libzmtp/src/zmtp_dealer.c           \
	  ./libzmtp/src/zmtp_endpoint.c         \
	  ./libzmtp/src/zmtp_ipc_endpoint.c     \
	  ./libzmtp/src/zmtp_msg.c              \
	  ./libzmtp/src/zmtp_tcp_endpoint.c

SRC_CLIENT = \
      libcrc/crc32.c             \
      src/voter_message.c        \
      src/ZMQ_Switcher.c         \
      src/voter_lib.c            \
      src/voter_logger.c         \
      clients/voter_client.c     \
	  rudp/rudp.c

# SRC_TCP_1_CLIENT = \
#       libcrc/crc32.c             \
#       src/voter_message.c        \
#       src/ZMQ_Switcher.c         \
#       src/voter_lib.c            \
#       src/voter_logger.c         \
#       clients/voter_client_tcp_1.c\
# 	  rudp/rudp.c

SRC_CLIENT_SENDER = \
      libcrc/crc32.c              \
      src/voter_message.c         \
      src/ZMQ_Switcher.c          \
      src/voter_lib.c             \
      src/voter_logger.c          \
      clients/client_sender.c     \
	  rudp/rudp.c

ifeq ($(TjComoFscpVoter),TjComoFscpVoter)
PhxpaxosLib = \
    -L $(ROOT)/external/phxpaxos/export_como -lPhxPaxosComo 			\
    -L $(ROOT)/external/phxpaxos/.lib -lphxpaxos  						\
    -L $(ROOT)/external/phxpaxos/third_party/leveldb -lleveldb 			\
    -L $(ROOT)/external/phxpaxos/third_party/protobuf/lib -lprotobuf	\
    -L $(ROOT)/external/phxpaxos/third_party/gflags/lib -lgflags 		\
    -L $(ROOT)/external/libzmq/build/lib -lzmq 							\
    $(ROOT)/bin/target/$(BUILD)/comort.so 	 							\
    -lpthread 															\
    -ldbus-1															\
    -lstdc++
else
PhxpaxosLib =
endif

SRC_O = $(patsubst %.c, %.o, $(SRC))
SRC_LIBZMTP_O = $(patsubst %.c, %.o, $(SRC_LIBZMTP))
SRCS = $(SRC) $(SRC_LIBZMTP)
OBJS = $(SRC_O) $(SRC_LIBZMTP_O)

como_fscp_voter: $(SRC) $(SRC_MAIN) $(SRC_LIBZMTP)
	$(CC) $(INC) $(SRC) $(SRC_MAIN) $(SRC_LIBZMTP) $(PhxpaxosLib) -o como_fscp_voter $(CFLAGS) $(CFLAGS2) $(MARCH_FLAG)

voter_client: $(SRC_CLIENT)
	$(CC) $(INC) $(SRC_CLIENT) $(SRC_LIBZMTP) $(PhxpaxosLib) -o voter_client $(CFLAGS) $(CFLAGS2)

# voter_client_tcp_1: $(SRC_TCP_1_CLIENT)
#	 $(CC) $(INC) $(SRC_TCP_1_CLIENT) $(SRC_LIBZMTP) -o voter_client_tcp_1 $(CFLAGS) $(CFLAGS2)

client_sender: $(SRC_CLIENT_SENDER)
	$(CC) $(INC) $(SRC_CLIENT_SENDER) $(SRC_LIBZMTP) $(PhxpaxosLib) -o client_sender $(CFLAGS) $(CFLAGS2)

%.o: %.c
	$(CC) $(INC) $(CFLAGS) $(CFLAGS2) $(MARCH_FLAG) -c $< -o $@

lib_como_fscp_voter: $(OBJS)
	$(AR) rcs lib_como_fscp_voter.a $(OBJS)

clean:
	rm -f como_fscp_voter
	rm -f voter_client
#	rm -f voter_client_tcp_1
	rm -f client_sender
	rm -f lib_como_fscp_voter
	rm -f $(OBJS)
